<?php

/*
 * Site login functions
 */
//Login to ft site then get page to save
function ft_login($url){
    global $mct_sl_optarray;
    $post_str = 'username='.$mct_sl_optarray['ft_uname'].'&password='.$mct_sl_optarray['ft_pass'];
    $cookie = ABSPATH.MCT_SL_SAVEPAGES.'/cookieft.txt';
    if (stripos(ABSPATH,'\\') !== false) {
         $cookie = str_ireplace('/','\\',$cookie); // Backslashed file system, so change all
    }
    $reg_url = 'https://registration.ft.com/registration/barrier/login';
    //check for bit.ly from email and get the redirect url before loggin in
    if (stripos($url,'on.ft.com') !== false){
        $ch = curl_init();
        // SET FILE TO DOWNLOAD
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        // EXECUTE HEADER REQUEST
        $hdr = curl_exec ($ch);
        // CLOSE CURL
        curl_close ($ch); 
        $headers = explode("\n",$hdr);
        // if there is no redirection this will be the final url
        $redir = $url;
        // loop through the headers and check for a Location: str
        $j = count($headers);
        for($i = 0; $i < $j; $i++){
        // if we find the Location header strip it and fill the redir var       
        if(strpos($headers[$i],"Location:") !== false){
                $redir = trim(str_replace("Location:","",$headers[$i]));
                break;
            }
        }
        $url = $redir;
    }
    return do_basic_login($url,$reg_url, $post_str, $cookie);
}

//WSJ Login function
function wsj_login($url){
    global $mct_sl_optarray;
    $post_str = 'user='.$mct_sl_optarray['wsj_uname'].'&password='.$mct_sl_optarray['wsj_pass'];
    $cookie = ABSPATH.MCT_SL_SAVEPAGES.'/cookiewsj.txt';
    if (stripos(ABSPATH,'\\') !== false) {
        $cookie = str_ireplace('/','\\',$cookie); // Backslashed file system, so change all
    }
    $reg_url = 'https://commerce.wsj.com/auth/submitlogin';
    return do_basic_login($url,$reg_url, $post_str, $cookie);
}
    
//NYT Login function
function nyt_login($url){
    global $mct_sl_optarray;
    $cookie = ABSPATH.MCT_SL_SAVEPAGES.'/cookienyt.txt';
    if (stripos(ABSPATH,'\\') !== false) {
        $cookie = str_ireplace('/','\\',$cookie); // Backslashed file system, so change all
    }
    // SET URL FOR THE POST FORM LOGIN and get the hidden fields
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://myaccount.nytimes.com/auth/login');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    // EXECUTE 1st REQUEST (get FORM LOGIN)
    $store = curl_exec ($ch);
    //Save the hidden tokens
    $token = stripos($store,'name="token" value="');
    $tval = substr($store,$token+20,32);
    $exp = stripos($store,'name="expires" value="');
    $eval = substr($store,$exp+22,10);
    curl_close($ch);
    //Now do the basic login
    $post_str = 'userid='.$mct_sl_optarray['nyt_uname'].'&password='.$mct_sl_optarray['nyt_pass'].'&is_continue=false&token='.$tval.'&expires='.$eval;
    $reg_url = 'https://myaccount.nytimes.com/auth/login';
    return do_basic_login($url,$reg_url, $post_str, $cookie);
}

function do_basic_login($url,$reg_url, $poststr,$cookie){
    
    // INIT CURL
    $ch = curl_init();
    // SET URL FOR THE POST FORM LOGIN
    curl_setopt($ch, CURLOPT_URL, $reg_url);
    // ENABLE HTTP POST
    curl_setopt ($ch, CURLOPT_POST, 1);
    // Don't worry about certificate
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    // SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststr);
    // IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
    //Force curl to return results and not display
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    // EXECUTE 1st REQUEST (FORM LOGIN)
    $store = curl_exec ($ch);
    curl_close($ch);
    //NOW GET THE PAGE
    $ch = curl_init();
    // SET FILE TO DOWNLOAD
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    // EXECUTE 2nd REQUEST (FILE DOWNLOAD)
    $content = curl_exec ($ch);
    // CLOSE CURL
    curl_close ($ch); 
    return $content;

    
}

?>
